package com.googlecode.gwtbb.client.shared.remote.user;

import com.googlecode.gwtbb.client.shared.GwtbbSmess;
import com.googlecode.gwtbb.client.shared.entity.user.UserBase;
import com.googlecode.gwtbb.client.shared.util.ObjectUtils0;
import com.googlecode.gwtbb.client.shared.util.StringUtils;
import com.googlecode.gwtbb.client.shared.validation.Failure;
import com.googlecode.gwtbb.client.shared.validation.FieldId;
import com.googlecode.gwtbb.client.shared.validation.GwtbbValidator;
import com.googlecode.gwtbb.client.shared.validation.Validateable;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jun 25, 2008
 */
public class RegisterUserBean implements Validateable, Serializable {
	public static final String PASSWORD_CONFIRM = "passwordConfirm";
	private String username;
	private String password;
	private String passwordConfirm;
	private String email;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordConfirm() {
		return passwordConfirm;
	}

	public void setPasswordConfirm(String passwordConfirm) {
		this.passwordConfirm = passwordConfirm;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Map<FieldId, Failure> validate(Map<FieldId, Failure> failures, int index) {
		username = GwtbbValidator.basicMinMax(failures, FieldId.create(UserBase.USERNAME, index), username, UserBase.USERNAME_MIN, UserBase.USERNAME_MAX);
		password = GwtbbValidator.basicMinMax(failures, FieldId.create(UserBase.PASSWORD, index), password, UserBase.PASSWORD_MIN, UserBase.PASSWORD_MAX);
		email = GwtbbValidator.basicMinMax(failures, FieldId.create(UserBase.EMAIL, index), email, UserBase.EMAIL_MIN, UserBase.EMAIL_MAX);
		passwordConfirm = StringUtils.aggressiveTrim(passwordConfirm);
		if (!ObjectUtils0.nullSafeEquals(password, passwordConfirm))
			failures.put(FieldId.create(PASSWORD_CONFIRM, index), new Failure(GwtbbSmess.instance().passwordMismatch()));
		return failures;
	}
}
